<?php
class VenuesController extends AppController {

	var $name = 'Venues';

	function index() {
		$this->Venue->recursive = 0;
		$this->set('venues', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid venue', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('venue', $this->Venue->read(null, $id));
	}

	function add($tab) {

		if (!empty($this->data)) {
			$this->Venue->create();
			if ($this->Venue->saveAll($this->data, array('validate' => 'first'))) {
				$this->Session->setFlash(__('The venue has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				header("HTTP/1.0 404 Not Found");
				header('Content-type: application/json');
				$errors=array();
				foreach($this->data as $k => $v) {
					if($k==$this->modelNames[0]) {
						$model_errors=$this->{$k}->validationErrors;
					} else {
						$model_errors=$this->{$this->modelNames[0]}->{$k}->validationErrors;
					}
					$errors[$k]=$model_errors;
				}

				$this->Session->setFlash(__('The venue could not be saved. Please, try again.', true));
				echo json_encode(array('errors' => $errors));
				exit;
			}
		}
		
		$markers = $this->Venue->Marker->find('list', array(
			'fields' => array('Marker.id', 'Marker.formatted_address'),
			'recursive' => 0
			));
		$users = $this->Venue->User->find('list', array(
			'fields' => array('User.id', 'UserProfile.name'),
			'conditions' => array('User.is_active' => 1),
			'recursive' => 0
			));
		$guides = $this->Venue->Guide->find('list');
		$countries = $this->Venue->VenueDetail->Country->find('list');
		$this->set(compact('markers', 'users', 'guides', 'countries','tab'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid venue', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Venue->save($this->data)) {
				$this->Session->setFlash(__('The venue has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The venue could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Venue->read(null, $id);
		}
		$markers = $this->Venue->Marker->find('list', array(
			'fields' => array('Marker.id', 'Marker.formatted_address'),
			'recursive' => 0
			));
		$users = $this->Venue->User->find('list', array(
			'fields' => array('User.id', 'UserProfile.name'),
			'conditions' => array('User.is_active' => 1),
			'recursive' => 0
			));
		$guides = $this->Venue->Guide->find('list');
		$countries = $this->Venue->VenueDetail->Country->find('list');
		$this->set(compact('markers', 'users', 'guides'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for venue', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Venue->delete($id)) {
			$this->Session->setFlash(__('Venue deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Venue was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
